﻿using StoryTeller.Infrastructure.DataAccess.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StoryTeller.Domain;
using StoryTeller.Infrastructure.DataAccess.DAL.Base;

namespace StoryTeller.Infrastructure.DataAccess.DAL
{
    public class ContributionDAL : BaseDAL, IContributionDAL
    {
        public void Insert(Contribution entidade)
        {
            Entities.Contribution.Add(entidade.ParseDomainToEntity());

            Save();
        }

        public void Update(Contribution entidade)
        {
            EContribution co = Entities.Contribution.First(c => c.Id == entidade.Id);

            co.LastUpdate = DateTime.Now;
            co.Text = entidade.Text;

            Save();
        }

        public void Delete(int id)
        {
            EContribution cat = Entities.Contribution.First(c => c.Id == id);

            if (cat != null)
            {
                Entities.Contribution.Remove(cat);

                Save();
            }
        }

        public ICollection<Contribution> Select()
        {
            ICollection<EContribution> coll = Entities.Contribution.ToList();

            return coll.Select(c => c.ParseEntityToDomain()).ToList();
        }

        public Contribution Select(int id)
        {
            return Entities.Contribution.First(c => c.Id == id).ParseEntityToDomain();
        }
    }
}
